package elitech.vietnam.myfashion.view;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Member;
import elitech.vietnam.myfashion.extend.ListFollowerArrayAdapter;
import elitech.vietnam.myfashion.fragment.MemberStyleFragment;

public class FollowingView extends LinearLayout implements OnItemClickListener{

	private MainActivity mContext;
	private ListView mListView;
	private ArrayList<Member> mListFollowing = new ArrayList<Member>();
	private ListFollowerArrayAdapter mAdapter;

	public FollowingView(MainActivity context) {
		super(context);
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.following, this);
		}
		this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		mListView = (ListView) this.findViewById(R.id.following_listview);

		mAdapter = new ListFollowerArrayAdapter(mContext, R.id.following_listview, mListFollowing, true);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		if (mListFollowing.size() == 0)
			GetFollowerStyle();
	}

	public void onResume() {
		
	}

	private void GetFollowerStyle() {
		new CallServiceTask() {
			ProgressDialog mProgress;
			@Override
			protected void onPreExecute() {
				mProgress = new ProgressDialog(mContext);
				mProgress.setMessage(mContext.getString(R.string.loading));
				mProgress.show();
			};
			@Override
			public void work() {
				try {
					mListFollowing.addAll(service.getFollowing(mContext.mUser.Id));
				} catch (IOException e) {
				} catch (XmlPullParserException e) {}
			}
			@Override
			protected void onPostExecute(Void result) {
				mAdapter.notifyDataSetChanged();
				if (mProgress.isShowing())
					mProgress.dismiss();
			}
		}.execute();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Log.w("Follow", "Item Click");
		if (mContext.mMemberStyleFragment == null)
			mContext.mMemberStyleFragment = new MemberStyleFragment();
		if (!mContext.mMemberStyleFragment.isAdded()) {
			mContext.mMemberStyleFragment.setMember(mListFollowing.get(position));
			mContext.replaceFragment(mContext.mMemberStyleFragment, true);
		}
	}
	

//	@Override
//	public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
//		if (mContext.mMemberStyleFragment == null)
//			mContext.mMemberStyleFragment = new MemberStyleFragment();
//		if (!mContext.mMemberStyleFragment.isAdded()) {
//			mContext.mMemberStyleFragment.setMember(mListFollowing.get(arg2));
//			mContext.replaceFragment(mContext.mMemberStyleFragment, true);
//		}
//	}
}
